#coding=utf-8
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow,QDialog,QMessageBox
from PyQt5.QtGui import QImage,QPixmap
from PyQt5 import QtWidgets
from ui import Ui_MainWindow as Cls_MainWindow
from e2e import E2E
import numpy as np
import cv2
class ClsWindow(QMainWindow, Cls_MainWindow):
    def __init__(self):
        super(ClsWindow, self).__init__()
        self.setupUi(self)
        self.img_path=''
        self.pushButton.clicked.connect(self.click_path)
        self.pushButton_2.clicked.connect(self.rec_dir)
        self.model=E2E()
    def click_path(self):
        sender = self.sender()
        #path = QtWidgets.QFileDialog.getExistingDirectory(self, "浏览", "D:\Program Files\ocr_demo\test_images")
        path,type=QtWidgets.QFileDialog.getOpenFileName(self, "浏览", "D:\Program Files\ocr_demo\test_images","All Files (*);;Text Files (*.txt)")
        self.img_path=path
        self.label.setText(self.img_path)
        label_width = self.label_img.width()
        label_height = self.label_img.height()
        img_src=cv2.imread(path)
        img_src=cv2.cvtColor(img_src,cv2.COLOR_BGR2RGB)
        temp_imgSrc = QImage(img_src[:], img_src.shape[1], img_src.shape[0], img_src.shape[1] * 3, QImage.Format_RGB888)
        pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)

        # 使用label进行显示
        self.label_img.setPixmap(pixmap_imgSrc)
    def rec_dir(self):
        im=cv2.imread(self.img_path)
        im, img_src = self.model.viz(im)

        label_width = self.label_img.width()
        label_height = self.label_img.height()
        im=cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
        temp_imgSrc = QImage(im, im.shape[1], im.shape[0], im.shape[1] * 3, QImage.Format_RGB888)
        pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)
        self.label_img.setPixmap(pixmap_imgSrc)

        label_width = self.label_res.width()
        label_height = self.label_res.height()
        temp_imgSrc = QImage(img_src[:], img_src.shape[1], img_src.shape[0], img_src.shape[1] * 3, QImage.Format_RGB888)
        pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)
        self.label_res.setScaledContents(True)
        # 使用label进行显示
        self.label_res.setPixmap(pixmap_imgSrc)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = ClsWindow()

    myWin.show()
    sys.exit(app.exec_())